package com.healthdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.healthdata.dao.MedicalInstitutionsRegionYearMapper;
import com.healthdata.entity.MedicalInstitutionsRegionYear;
import com.healthdata.service.MedicalInstitutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MedicalInstitutionServiceImpl implements MedicalInstitutionService{

    @Autowired
    private MedicalInstitutionsRegionYearMapper medicalInstitutionsRegionYearMapper;

    public Map<String, Object> getMedicalData() {
        // 查询2011到2020年所有的数据
        List<MedicalInstitutionsRegionYear> dataList =
                medicalInstitutionsRegionYearMapper.selectList(
                        new QueryWrapper<MedicalInstitutionsRegionYear>().in("year", 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020));

        // 将查询结果按年份分组
        Map<String, Object> groupedData = new HashMap<>();

        for (MedicalInstitutionsRegionYear record : dataList) {
            // 将查询结果封装成前端需要的格式
            Map<String, Object> map = new HashMap<>();
            map.put("name", record.getRegion());  // 省份名称
            map.put("value", record.getTotal());  // 医疗机构数量

            // 按年份将数据分组
            String yearKey = "data" + record.getYear();

            // 将年份的数据放入map中
            if (!groupedData.containsKey(yearKey)) {
                groupedData.put(yearKey, new ArrayList<Map<String, Object>>());
            }
            ((List<Map<String, Object>>)groupedData.get(yearKey)).add(map);
        }

        return groupedData;
    }

}
